﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxCallback;
using DevExpress.Web.ASPxClasses;
using SGRapidForm.Permission.Service.Interface;
using SGRapidForm.Common;
using System.Collections.Specialized;
using SGRapidForm.Permission.Entity;
using System.Net.Mail;
using System.Text;

namespace SGDemo.Web
{
    public partial class RetrievePassword : System.Web.UI.Page
    {
        private IPermissionService _permissionService = ServiceProxyFactory.Create<IPermissionService>("permissionservice");

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        /// <summary>
        /// 执行动作
        /// </summary>
        protected void cbpRetrievePasswordPanel_Callback(object sender, CallbackEventArgsBase e)
        {
            NameValueCollection parameters = HttpUtility.ParseQueryString(e.Parameter);

            if (parameters["Step"]=="1")
            {
                //验证参数是为否空
                if (string.IsNullOrEmpty(parameters[0]) || string.IsNullOrEmpty(parameters[1]) || string.IsNullOrEmpty(parameters[2]))
                {
                    throw new ArgumentNullException("用户名和验证码等参数不能为空！");
                }
                //验证验证码是否正确
                if (parameters["Code"] != capCode.Code)
                {
                    lblTipsA.Text = "提示：验证码输入错误!";
                    return;
                }

                //验证用户名是否存在
                if (!_permissionService.User_IsExist(parameters["UserName"]))
                {
                    lblTipsA.Text = "提示：用户名不存在!";
                    return;
                }

                hfUserName.Value = parameters["UserName"];
                UserDetails user = _permissionService.User_GetByName(hfUserName.Value);
                txtPasswordQuestion.Text = user.PasswordQuestion;

                pnlFirst.Visible = false;
                pnlSecond.Visible = true;
            }
            else if (parameters["Step"] == "2")
            {
                //验证参数是为否空
                if (string.IsNullOrEmpty(parameters[0]) || string.IsNullOrEmpty(parameters[1]))
                {
                    throw new ArgumentNullException("密保答案等参数不能为空！");
                }

                UserDetails user = _permissionService.User_GetByName(hfUserName.Value);
                if (user.PasswordAnswer != parameters["PasswordAnswer"])
                {
                    lblTipsB.Text = "提示：密保答案错误!";
                    txtPasswordQuestion.Text = user.PasswordQuestion;
                    
                    pnlFirst.Visible = false;
                    pnlSecond.Visible = true;
                    return;
                }

                SendEmail(user);
            }
        }

        /// <summary>
        /// 给用户发送邮件
        /// </summary>
        /// <param name="user"></param>
        protected void SendEmail(UserDetails user)
        {
            try
            {
                MailMessage msg = new MailMessage();
                msg.IsBodyHtml = true;
                msg.From = new MailAddress("xmhhzx@163.com", "辉煌装修信息管理系统");
                msg.To.Add(user.Email.Trim());
                msg.Subject = "用户名密码取回-辉煌装修信息管理系统";
                StringBuilder body = new StringBuilder();
                body.AppendLine("您的密码已经成功找回<br/>");
                body.AppendFormat("用户名：{0}<br/>", user.Username);
                body.AppendFormat("密&nbsp;&nbsp;码：{0}<br/>", DESEncrypt.Decrypt(user.Password));
                body.AppendLine("请妥善保管好您的帐号信息<br/>");
                body.AppendLine("<b>请勿回复此邮件！</b><br/>");
                msg.Body = body.ToString();

                SmtpClient client = new SmtpClient();
                client.Send(msg);

                lblTipsC.Text = "<p><img src=\"Images/Common/yes.png\" /> <b>发送成功！</b></p><p>提示：邮件已经成功发送到邮箱：" + user.Email + "， 请及时查收。</p>";
               
            }
            catch (Exception e)
            {
                lblTipsC.Text = "提示：发送邮件失败！";
            }
            finally
            {
                pnlFirst.Visible = false;
                pnlSecond.Visible = false;
                pnlThird.Visible = true;
            }
        }
    }
}